<template>
  <ul class="app-header-nav">
    <li class="home">
      <RouterLink to="/">首页</RouterLink>
    </li>
    <li v-for="item in list" :key="item.id" @mousemove="show(item)" @mouseleave="hide(item)">
      <RouterLink @click="hide(item)" :to="`/category/${item.id}`">{{ item.name }}</RouterLink>
      <div class="layer" :class="{ open: item.open }">
        <ul>
          <li v-for="sub in item.children" :key="sub.id">
            <RouterLink @click="hide(item)" :to="`/category/sub/${sub.id}`">
              <img :src="sub.picture" alt="">
              <p>{{ sub.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script setup >

import { useStore } from 'vuex'
const store = useStore()
const list = computed(() => { // 2.0的时候也是同理使用mapState映射到computed里面
  return store.state.category.list
})
// 存在问题，跳转后不会关闭二级分类弹层// 解决思路，给分类数据添加open属性，从而通过控制open来控制弹层显示与隐藏
const show = (item) => {
  store.commit('category/show', item.id)
}
const hide = (item) => {
  store.commit('category/hide', item.id)
}
</script>

<style scoped lang='less'>
.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;

  >li {
    margin-right: 40px;
    width: 38px;
    text-align: center;

    >a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }

    &:hover {

      >a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }

      // > .layer { // 鼠标经过时显示元素
      //   height: 132px;
      //   opacity: 1;
      // }
    }

  }
}

.layer {
  &.open {
    // 当layer有open时显示
    height: 132px;
    opacity: 1;
  }

  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all .3s .1s;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;

    li {
      width: 110px;
      text-align: center;

      img {
        width: 60px;
        height: 60px;
      }

      p {
        padding-top: 10px;
      }

      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
